package com.sanjiahantang.youjiarole.module.join.adapters;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.join.beans.ImageBean;
import com.sanjiahantang.youjiarole.module.join.presenters.CertificationPresenter;
import com.sanjiahantang.youjiarole.module.join.views.impl.CertificationActivity;
import com.sanjiahantang.youjiarole.utils.FileUtils;
import com.sanjiahantang.youjiarole.utils.SPUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by zy on 2017/10/26.
 */

public class ImageAddAdapter extends BaseAdapter {

    private List<ImageBean> data;
    private Context context;
    private LayoutInflater inflater;
    private CertificationPresenter presenter;
    private int type;
    private String token;
    private String rolePassed;

    public static final int TYPE_ID_CARD = 0;
    public static final int TYPE_MANAGER = 1;
    public static final int TYPE_SUPERVISION = 2;
    public static final int TYPE_SHOP = 3;
    private boolean showCloseBtn = true;
    private int limitSize;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public ImageAddAdapter(Context context, CertificationPresenter presenter, int type){
        this.data = new ArrayList<>();
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.presenter = presenter;
        this.type = type;
        rolePassed = SPUtils.get(context, SPConfig.ROLE_ID,"").toString();
        limitSize = 0;
        if (type == TYPE_ID_CARD){
            limitSize = 2;
        }


        if (!TextUtils.isEmpty(rolePassed)){

            if (type == TYPE_ID_CARD){
                showCloseBtn = false;
            }

            if (rolePassed.contains("1") && type == TYPE_MANAGER){
                showCloseBtn = false;
            }

            if (rolePassed.contains("2") && type == TYPE_SUPERVISION){
                showCloseBtn = false;
            }

            if (rolePassed.contains("3") && type == TYPE_SHOP){
                showCloseBtn = false;
            }
        }
    }

    @Override
    public int getCount() {
        if (showCloseBtn){
            if (data.size() >= limitSize && limitSize > 0){
                return data == null ? 0 : (data.size() + 0);
            }else{
                return data == null ? 1 : (data.size() + 1);
            }
        }else{
            return data == null ? 0 : (data.size() + 0);
        }
    }

    @Override
    public Object getItem(int i) {
        if (showCloseBtn){
            if (data.size() >= limitSize && limitSize > 0){
                return data.get(i);
            }else{
                return i == getCount() - 1 ? null : data.get(i);
            }
        }else{
            return data.get(i);
        }
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public int getRealSize(){
        return data == null ? 0 : data.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (position < data.size()){
            convertView = inflater.inflate(R.layout.item_image, null);
        }else{
            convertView = inflater.inflate(R.layout.item_add_image, null);
        }

        holder = new ViewHolder(convertView);

        if (getItem(position) instanceof ImageBean){
            if (data.get(position).getType() == ImageBean.TYPE_PATH){
                holder.bind(data.get(position).getPath(),position);
            }
        }else if(getItem(position) == null){
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((CertificationActivity)context).showAtLocation(type);
                }
            });
        }

        return convertView;
    }

    private class ViewHolder{
        private View itemView;
        public ViewHolder(View itemView){
            this.itemView = itemView;
        }

        public void bind(final String item, final int position) {
            ImageView iv = (ImageView) itemView.findViewById(R.id.image);
            Picasso.with(context).load(FileUtils.rePath(item)).resize(Utils.dip2px(context,50),Utils.dip2px(context,50)).into(iv);


            View v = itemView.findViewById(R.id.ivClose);
            if (showCloseBtn){
                v.setVisibility(View.VISIBLE);
            }else{
                v.setVisibility(View.GONE);
            }
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    presenter.deleteFile(context,data.get(position).getId(),token, new BaseView.OnLoadListener() {
                        @Override
                        public void onLoad(Object content) {
                            data.remove(position);
                            notifyDataSetChanged();
                        }
                    });
                }
            });

            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Utils.showPicDialog((Activity) context,FileUtils.rePath(item),R.drawable.i_rect_bg);
                }
            });
        }
    }

    public void addAll(Collection<? extends ImageBean> list){
        data.addAll(list);
        notifyDataSetChanged();
    }

    public void add(ImageBean url){
        data.add(url);
        notifyDataSetChanged();
    }

    public void clear(){
        data.clear();
        notifyDataSetChanged();
    }
}
